using System.IO;
using System.Windows.Forms;
using HogoLogic;

namespace ProjectView
{
	enum SelectProjectResult
	{
		Empty,
		Init,
		Checkout,
		Make,
		Open
	}

	internal partial class ProjectViewForm : Form
	{
		private HogoProject newProject;
		static FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
		
		public ProjectViewForm(HogoProjectList projects)
		{
			InitializeComponent();

			ShowProjects(projects);
			SetupUI(projects);
		}

		private void SetupUI(HogoProjectList projects)
		{
			//Set "open project" radio button enable
			openRadio.Enabled = projects != null && projects.Projects.Count > 0;

			//choose default variant
			if (openRadio.Enabled)
				openRadio.Checked = true;
			else
				initRadio.Checked = true;
		}

		private void ShowProjects(HogoProjectList projects)
		{
			if (projects != null)
				foreach (HogoProject project in projects.Projects)
				{
					ListViewItem projectListViewItem = new ListViewItem();
					projectListViewItem.Text = project.ProjectFolder;
					projectListViewItem.ImageIndex = 0;
					projectListViewItem.Tag = project;					

					projectsListView.Items.Add(projectListViewItem);
				}
		}

		internal SelectProjectResult Result
		{
			get
			{
				if (initRadio.Checked)
					return SelectProjectResult.Init;
				if (checkoutRadio.Checked)
					return SelectProjectResult.Checkout;
				if (makeRadio.Checked)
					return SelectProjectResult.Make;
				if (openRadio.Checked)
					return SelectProjectResult.Open;
				return SelectProjectResult.Empty;
			}
		}

		internal HogoProject SelectedProject
		{
			get
			{
				if (projectsListView.SelectedItems.Count > 0)
					return projectsListView.SelectedItems[0].Tag as HogoProject;
				else
					return null;
			}
		}

		public HogoProject NewProject
		{
			get { return newProject; }
		}


		private void okButton_Click(object sender, System.EventArgs e)
		{
			newProject = null;
			
			if (Result == SelectProjectResult.Open)
			{
				this.DialogResult = DialogResult.OK;
				Close();
				
			}
			else
			{
				if (Result == SelectProjectResult.Make)
					newProject = MakeProject();

				if (Result == SelectProjectResult.Init)
					newProject = InitProject();
				
				if (newProject != null)
				{
					this.DialogResult = DialogResult.OK;
					Close();
				}
			}
		}

		private HogoProject InitProject()
		{
			if (folderBrowser.ShowDialog() == DialogResult.OK)
			{
				if (Directory.Exists(Path.Combine(folderBrowser.SelectedPath, ".hg")))
				{
					MessageBox.Show("There is already mercurial repository  in this folder");
					return null;
				}
				else
				{
					return HogoProject.InitProject(folderBrowser.SelectedPath);
				}
			}
			else
				return null;
		}

		private static HogoProject MakeProject()
		{
			if (folderBrowser.ShowDialog() == DialogResult.OK)
			{
				if (Directory.Exists(Path.Combine(folderBrowser.SelectedPath, ".hg")))
					return new HogoProject(folderBrowser.SelectedPath);
				else
				{
					MessageBox.Show("It's not a folder with mercurial repository");
					return null;
				}
			}
			else
				return null;
		}
	}
}